<template>
	<view class="main">
		<view class="bg"></view>
		<view class="nabbar" :style="{'height': statusBarHeight + barHeight + 'px'}">
			<!-- //手机状态栏 -->
			<view class="status-bar" :style="{'height': statusBarHeight + 'px'}"></view>
			<!-- //导航栏 -->
			<view class="nabbar-box" :style="{ 'top': statusBarHeight+ 'px', 'heigth': barHeight + 'px' }">
				<view class="nabbar-cont">
					<view @click="toLogin()" style="position: absolute;left: 20rpx;color: black;font-size: 28rpx;">
						退出登录
					</view>
					<view style="color: black;">部门管理员</view>
				</view>
			</view>
		</view>
		<view :style="{'height':barHeight+'px'}"></view>
		<view class="bookBtn bookBtn2" @click="toBind()">{{wxapp_openid==''?'绑定':'解绑'}}微信</view>
		<view class="bookBtn" @click="toBook()">订阅消息</view>
		<!-- <view class="bookBtn bookBtn3" @click="toBook()">退出登录</view> -->
		<view class="flex align between" style="margin: 0 60rpx;">
			<view class="item2" @click="toUrl(1)">
				<image mode="widthFix" src="/static/images/icon9.png"></image>
				<view>流出党员数据统计</view>
			</view>
			<view class="item2" @click="toUrl(2)">
				<image mode="widthFix" src="/static/images/icon8.png"></image>
				<view>流入党员数据统计</view>
			</view>

		</view>
		<view class="item flex align" @click="toUrl(3)">
			<image src="/static/images/icon6.png"></image>
			流动消息
		</view>
		<view class="item flex align" @click="toUrl(4)">
			<image src="/static/images/icon10.png"></image>
			困难诉求
		</view>
		<view class="item flex align" @click="toUrl(5)">
			<image src="/static/images/icon11.png"></image>
			发布通知
		</view>
		<view class="item flex align" @click="toUrl(6)">
			<image src="/static/images/icon7.png"></image>
			视频会议
		</view>
	</view>
</template>

<script>
	import {
		adminWxbind,
		adminDetail,
		adminWxunbind,
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				wxapp_openid: '',
				statusBarHeight: 20,
				barHeight: 44,
				barWidth: null,
			};
		},
		onShow() {
			let token = uni.getStorageSync('token2')
			if (token) {
				uni.request({
					url: 'https://xsdy.ncscloudmedia.com/ymadmin.php/admin/checkToken',
					data: {},
					method: 'POST',
					header: {
						'content-type': 'application/json',
						'token': token
					},
					success: result => {
						if (result.data.code == 1) {
							this.getDetail()
						} else {
							uni.reLaunch({
								url: '/pages/login/loginM'
							})
						}
					},
					fail: result => {
						uni.hideLoading();
					}
				})
			} else {
				uni.reLaunch({
					url: '/pages/login/loginM'
				})
			}
		},
		onLoad() {
			let that = this;
			//通过uni自带api获取手机系统信息
			uni.getSystemInfo({
				success: function(res) {
					// console.log(res)
					that.statusBarHeight = res.statusBarHeight; //手机状态栏高度
					let isiOS = res.system.indexOf('iOS') > -1; //是否为iOS系统
					that.barHeight = !isiOS ? 48 : 44; //导航栏高度，iOS：48，Android：44
					that.barWidth = res.windowWidth - 87; //nabbar可操作宽度 = 屏幕宽度 - 小程序右上角的胶囊宽度（87）
				}
			})
		},
		methods: {
			toLogin(){
				uni.showModal({
					title: '提示',
					content: '确定要退出该账号吗？',
					success: function(res) {
						if (res.confirm) {
							uni.removeStorageSync('token2')
							uni.navigateTo({
								url:'/pages/login/loginM'
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			toUrl(n) {
				// if (n == 6) {
				// 	uni.navigateToMiniProgram({
				// 		appId: 'wx33fd6cdc62520063',
				// 		path: '',
				// 		success(res) {}
				// 	});
				// }
				if (n == 1 || n == 2) {
					uni.navigateTo({
						url: '/pages/manage/manage?type=' + n
					})
				}
				if (n == 3) {
					uni.navigateTo({
						url: '/pages/manage/applyList'
					})
				}
				if (n == 4) {
					uni.navigateTo({
						url: '/pages/manage/suggestList'
					})
				}
				if (n == 5) {
					uni.navigateTo({
						url: '/pages/manage/send'
					})
				}
				if (n == 6) {
					uni.navigateTo({
						url: '/pages/manage/videot'
					})
				}

			},
			// 获取用户详情
			async getDetail() {
				const result = await adminDetail()
				if (result.code == 1) {
					if (result.data.wxapp_openid != null) {
						this.wxapp_openid = result.data.wxapp_openid
					}
				}
			},
			// 订阅消息
			toBook() {
				if (this.wxapp_openid == '') {
					uni.showModal({
						title: '提示',
						content: '请先绑定微信',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								// console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					let that = this
					wx.getSetting({
						withSubscriptions: true,
						success(res) {
							let templateId = "eAvm1oL34Z7_IVf4D7jblQsaWvXYCfR8lIKe31nft-0"; //替换为你的模板ID
							let subscriptionStatus = res.subscriptionsSetting[templateId];
							if (subscriptionStatus === "accept") {
								//用户已订阅
								console.log("用户已订阅");
								uni.showModal({
									title: '提示',
									content: '您已订阅该消息',
									showCancel: false,
									success: function(res) {
										if (res.confirm) {
											// console.log('用户点击确定');
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							} else if (subscriptionStatus === "reject") {
								//用户拒绝订阅
								console.log("用户拒绝订阅");
								that.toBookCom()
							} else if (subscriptionStatus === "ban") {
								//订阅消息被封禁
								console.log("订阅消息被封禁");
								that.toBookCom()
							} else {
								//用户未订阅或未授权
								console.log("用户未订阅或未授权");
								that.toBookCom()
							}

						}

					});
				}
			},
			toBookCom() {
				let id = "eAvm1oL34Z7_IVf4D7jblQsaWvXYCfR8lIKe31nft-0"; // 替换你自己的模板ID 模板详情那里就有模板ID
				// 调用订阅消息API
				let that = this
				uni.requestSubscribeMessage({
					tmplIds: [id], // 这里填写你的模板ID数组
					success(res) {
						if (res[id] === 'accept') {
							// 用户同意订阅该模板
							uni.showToast({
								title: "订阅成功",
								icon: "success",
							});
						}
						if (res[id] === 'reject') {
							// 用户拒绝订阅该模板
							uni.showModal({
								title: "订阅失败",
								content: "您已设置不再询问，请前往设置开启订阅提醒。",
								confirmText: "去设置",
								success(modalRes) {
									if (modalRes.confirm) {
										that.openSetting();
									} else {
										uni.showToast({
											title: "您可能错过重要通知",
											icon: "none",
										});
									}
								},
							});
						}
					},
					fail(err) {
						console.error('订阅消息失败', err);
					}
				});

			},
			openSetting() {
				uni.openSetting({
					success(res) {
						console.log("设置界面返回：", res);
						if (res.subscriptionsSetting) {
							console.log("订阅消息授权状态：", res.subscriptionsSetting);
						}
					},
					fail(err) {
						console.error("打开设置失败：", err);
					},
				});
			},
			// 绑定微信
			toBind() {
				let that = this
				if (that.wxapp_openid == '') {
					uni.login({
						provider: 'weixin', //使用微信登录
						success(lres) {
							console.log(lres, '获取到的code--------1');
							let code = lres.code
							that.wxbind(code)
						}
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '确定要解绑微信吗？',
						success: function(res) {
							if (res.confirm) {
								that.wxunbind()
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}

			},
			async wxbind(code) {
				const result = await adminWxbind({
					code: code
				})
				if (result.code == 1) {
					uni.showToast({
						title: result.msg
					})
					setTimeout(() => {
						this.getDetail()
					}, 1000)
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					})
				}
			},
			async wxunbind() {
				const result = await adminWxunbind()
				if (result.code == 1) {
					uni.showToast({
						title: result.msg
					})
					setTimeout(() => {
						this.getDetail()
					}, 1000)
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bg {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: -1;
		background: linear-gradient(to bottom, #FEEEE1, #F3F4F6) !important;
	}

	.bookBtn {
		position: fixed;
		top: 600rpx;
		right: 0;
		background: #55aaff;
		color: white;
		padding: 20rpx 20rpx;
		border-radius: 8rpx 0 0 8rpx;
		font-size: 32rpx;
		font-weight: bold;
		width: 20rpx;
	}

	.bookBtn2 {
		top: 350rpx;
		background: #55aa7f;
	}
	.bookBtn3{
		top: 800rpx;
	}

	.main {
		position: relative;
		padding-top: 120rpx;
	}

	.item {
		background: white;
		text-align: center;
		border-radius: 8rpx;
		color: #FF9008;
		font-size: 36rpx;
		margin: 0 auto 60rpx;
		padding: 30rpx 65rpx;
		width: fit-content;
		text-align: center;
		box-shadow: 1rpx 1rpx 10rpx 5rpx gainsboro;
		text {
			display: block;
			font-size: 24rpx;
			color: #532f02;
			margin-top: 10rpx;
		}
		image{
			width: 115rpx;
			height: 89rpx;
			margin-right: 92rpx;
		}
	}

	.item2 {
		width: 40%;
		font-size: 28rpx;
		text-align: left;
		padding: 20rpx;
		color: #FF9008;
		background: white;
		margin-bottom: 60rpx;
		box-shadow: 1rpx 1rpx 10rpx 5rpx gainsboro;
		border: solid 1rpx #FF9307;
		border-radius: 8rpx;
		image {
			width: 60rpx;
			height: 68rpx;
		}
	}
</style>